﻿using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using ProjBackEnd.Areas.Sys.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Sys.Controllers
{
    /// <summary>
    /// 公司操作
    /// </summary>
    public class ComInfoController : ApiController
    {   
        /// <summary>
        /// 获取公司列表
        /// </summary>
        /// <returns>自定义结果集</returns>
        [HttpGet]
        [Route("api/sys/cominfo")]
        public RespSysComInfo GetComSummaryList()
        {
            RespSysComInfo resp = new RespSysComInfo();
            List<ComSummaryInfo> lst = basecodeLogic.GetComSummary();

            if (lst.Count == 0)
            {
                resp.statusCode = 60017;
                resp.statusMsg = "获取基础代码失败";
                return resp;
            }

            ResultComSummary res = new ResultComSummary();
            res.comsummarylist = lst;
            resp.result = res;
            resp.statusCode = 0;
            resp.statusMsg = "";
            return resp;
        }

        /// <summary>
        /// 按集团代码获取公司列表
        /// </summary>
        /// <param name="key">集团代码</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/sys/gpcominfo")]
        public RespSysComInfo GetGpComSummaryList(string key)
        {
            RespSysComInfo resp = new RespSysComInfo();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }
            string strGpcd = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt);
            List<ComSummaryInfo> lst = basecodeLogic.GetComSummary(strGpcd);

            if (lst.Count == 0)
            {
                resp.statusCode = 60017;
                resp.statusMsg = "获取基础代码失败";
                return resp;
            }

            ResultComSummary res = new ResultComSummary();
            res.comsummarylist = lst;
            resp.result = res;
            resp.statusCode = 0;
            resp.statusMsg = "";
            return resp;
        }

        /// <summary>
        /// 更新公司信息
        /// </summary>
        /// <param name="_reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/sys/cominfo")]
        public RespSysNullResultInfo UpdateComInfo(ReqComInfo _reqInfo)
        {
            RespSysNullResultInfo resp = new RespSysNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            return basecodeLogic.UpdateComInfo(_reqInfo.cominfo);
        }

        /// <summary>
        /// 获取集团列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("api/sys/gpinfo")]
        public RespSysGroupInfo GetGroupList()
        {
            RespSysGroupInfo resp = new RespSysGroupInfo();
            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            return basecodeLogic.GetGroupList();
        }

        /// <summary>
        /// 更新集团信息
        /// </summary>
        /// <param name="_reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/sys/gpinfo")]
        public RespSysNullResultInfo UpdateGroupInfo(ReqGroupInfo _reqInfo)
        {
            RespSysNullResultInfo resp = new RespSysNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            return basecodeLogic.UpdateGroupInfo(_reqInfo.gpinfo);
        }


    }
}
